package com.carey.blog.action.impl;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;

import com.carey.blog.framework.latke.action.ActionException;
import com.carey.blog.framework.latke.util.Strings;
import com.carey.blog.model.ErrorPage;

/**
 * Admin console error action. admin-error.ftl.
 * 
 */
public final class AdminErrorAction extends AbstractAdminAction {

	/**
	 * Default serial version uid.
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * Logger.
	 */
	private static final Logger LOGGER = Logger
			.getLogger(AdminErrorAction.class.getName());

	@Override
	protected Map<?, ?> doFreeMarkerAction(
			final freemarker.template.Template template,
			final HttpServletRequest request, final HttpServletResponse response)
			throws ActionException {
		final Map<String, Object> ret = new HashMap<String, Object>();

		try {
			String requestURI = request
					.getParameter(ErrorPage.ERROR_PAGE_REQUEST_URI);
			if (Strings.isEmptyOrNull(requestURI)) {
				requestURI = (String) request
						.getAttribute(ErrorPage.ERROR_PAGE_REQUEST_URI);
				if (Strings.isEmptyOrNull(requestURI)) {
					requestURI = "Unknown";
				}
			}
			ret.put(ErrorPage.ERROR_PAGE_REQUEST_URI, requestURI);

			String cause = request.getParameter(ErrorPage.ERROR_PAGE_CAUSE);
			if (Strings.isEmptyOrNull(cause)) {
				cause = (String) request
						.getAttribute(ErrorPage.ERROR_PAGE_CAUSE);
				if (Strings.isEmptyOrNull(cause)) {
					cause = "Unknown";
				}
			}
			ret.put(ErrorPage.ERROR_PAGE_CAUSE, cause);
		} catch (final Exception e) {
			LOGGER.log(Level.SEVERE, e.getMessage(), e);
			throw new ActionException(e);
		}

		return ret;
	}

	@Override
	protected void doPost(final HttpServletRequest request,
			final HttpServletResponse response) throws ServletException,
			IOException {
		doGet(request, response);
	}

	@Override
	protected JSONObject doAjaxAction(final JSONObject data,
			final HttpServletRequest request, final HttpServletResponse response)
			throws ActionException {
		throw new UnsupportedOperationException("Not supported yet.");
	}
}
